package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.RecruitStuMapper;
import org.hnxxxy.ebms.mapper.RoleMapper;
import org.hnxxxy.ebms.mapper.StudentMapper;
import org.hnxxxy.ebms.mapper.TeacherAdmissMapper;
import org.hnxxxy.ebms.pojo.Plan;
import org.hnxxxy.ebms.pojo.Role;
import org.hnxxxy.ebms.pojo.Student;
import org.hnxxxy.ebms.pojo.TeacherAdmiss;
import org.hnxxxy.ebms.service.RoleService;
import org.hnxxxy.ebms.service.StudentService;
import org.hnxxxy.ebms.util.CommonUntil;
import org.hnxxxy.ebms.vo.StudentResponseVo;
import org.hnxxxy.ebms.vo.TeaStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-15
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private TeacherAdmissMapper teacherAdmissMapper;

    @Autowired
    private RecruitStuMapper recruitStuMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RoleService roleService;

    @Override
    public Result<String> add(Student student) {
        Role role = roleMapper.selectById(CommonUntil.getId());
        student.setRecruitStuId(role.getUserId());
        this.save(student);
        return Result.success(200,"添加成功");
    }

    @Override
    public List<StudentResponseVo> getStu() {
        Role role = roleMapper.selectById(CommonUntil.getId());
        if (role.getIsTeacher()){
            return studentMapper.selectStu(role.getUserId());
        }else {
            // 通过老师id查询到地区id，
            List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
            // 通过地区id查询到学生id，
            List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
            return studentMapper.selectTeaStu(recId);
        }
    }

    @Override
    public void addBatch(List<Student> list) {
        List<Student> students = new ArrayList<>();
        list.forEach(student -> {
            student.setRecruitStuId(CommonUntil.getId());
            students.add(student);
        });
        this.saveBatch(students);
    }

    @Override
    public Result<IPage<StudentResponseVo>> getTeaTudent(int page) {
        IPage<StudentResponseVo> iPage = new Page<>(page,10);
        Role role = roleMapper.selectById(CommonUntil.getId());
        // 通过老师id查询到地区id，
        List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
        // 通过地区id查询到学生id，
        List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
        if (recId == null || recId.size() ==0){
            return Result.success(null);
        }
        IPage<StudentResponseVo> teaStudentVoIPage = studentMapper.getTeaTudent(iPage,recId);
        return Result.success(teaStudentVoIPage);
    }

    @Override
    public Role getRole(Integer id) {
        return roleService.getById(id);
    }

    @Override
    public Result<IPage<Student>> getStudents(int page) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        // 如果是老师的话就查询当前地区所有收集到的学生资料
        if (role.getIsTeacher().equals(0)){
            return getStuByTeacher(page);
        }else {
            // 否则就查询当前学生
            return getStuByRec(page);
        }
    }

    /**
     * 查询招生学生收集到的学生信息
     * @param page
     * @return
     */
    private Result<IPage<Student>> getStuByRec(int page) {
        Integer id = CommonUntil.getId();
        Role role = roleService.getById(id);
        IPage<Student> iPage = new Page<>(page,10);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruit_stu_id",role.getUserId()).orderByAsc("student_relation");
        IPage<Student> studentIPage = this.baseMapper.selectPage(iPage, queryWrapper);
        return Result.success(studentIPage);
    }

    /**
     * 查询招生老师所属地区的学生信息
     * @param page
     * @return
     */
    private Result<IPage<Student>> getStuByTeacher(int page) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        // 通过老师id查询到地区id，
        List<Integer> admissId = teacherAdmissMapper.selectByTeaId(role.getUserId());
        // 通过地区id查询到学生id，
        List<Integer> recId = recruitStuMapper.selectByAdmissId(admissId);
        // 然后通过学生id查询到收集的学生信息
        IPage<Student> iPage = new Page<>(page,10);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("recruit_stu_id",recId);
        IPage<Student> iPage1 = this.baseMapper.selectPage(iPage, queryWrapper);
        return Result.success(iPage1);
    }
}
